package smetana.modules.ui.form.search;

import com.google.inject.Inject;
import org.apache.pivot.collections.List;
import org.apache.pivot.wtk.Dialog;
import smetana.domain.raw.Manager;
import smetana.modules.business.BusinessManager;
import smetana.modules.business.BusinessPivot;

/**
 *
 * @author dmitry.mamonov
 */
public class SearchManagerDialog extends AbstractSearchPanel<Manager>{
    Dialog dialog;

    @Inject
    BusinessManager businessManager;

    @Inject
    BusinessPivot businessPivot;

    @Override
    protected Manager createNew(String text) {
        return businessManager.newManager(text);
    }

    @Override
    protected void edit(Manager current, String newText) {
        Manager manager = businessManager.findManagerById(current.getId());
        manager.setManagerName(newText);
        businessManager.saveManager(manager);
    }

    @Override
    protected List<Manager> load() {
        return businessPivot.pivotListOfManagers();
    }

    @Override
    protected String toString(Manager value) {
        return value.getManagerName();
    }

    @Override
    protected void performSelect() {
        dialog.close(getResult()!=null);
    }

    @Override
    public void init() {
        super.init();
        dialog = new Dialog("Выбор бригадира", this.getComponent());
    }

    public Dialog getDialog() {
        initialize();
        return dialog;
    }
}
